﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace SBarth.TheLionsShare
{
    public static class CommonUtils {

        public static void SendMail(string replyToAddress, string body)
        {
            string emailServerHost = ConfigurationManager.AppSettings["ContactUs_Email_Server_Host"].ToString();
            int emailServerPort = Convert.ToInt32(ConfigurationManager.AppSettings["ContactUs_Email_Server_Port"].ToString());
            string toAddress = ConfigurationManager.AppSettings["ContactUs_Email_To"].ToString();
            string fromAddress = ConfigurationManager.AppSettings["ContactUs_Email_From"].ToString();
            string subject = ConfigurationManager.AppSettings["ContactUs_Email_Subject"].ToString();
            string content = HttpUtility.HtmlEncode(ConfigurationManager.AppSettings["ContactUs_Email_Body_Header"].ToString() + "\n\n" + 
                             body + "\n\n" + 
                             ConfigurationManager.AppSettings["ContactUs_Email_Body_Footer"].ToString());

            MailMessage msg = new MailMessage();
            SmtpClient smtp = new SmtpClient();

            try 
            {	        
                msg.To.Add(toAddress);
                msg.From = new MailAddress(fromAddress);
                msg.Sender = new MailAddress(fromAddress);
                msg.ReplyTo = new MailAddress(replyToAddress);
                msg.Priority = MailPriority.Normal;
                msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                msg.IsBodyHtml = false;
                msg.IsBodyHtml = false;
                msg.Subject = subject;
                msg.Body = content;

                smtp.Host = emailServerHost;
                smtp.Port = emailServerPort;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Credentials = CommonUtils.IsEmptyOrNull(ConfigurationManager.AppSettings["ContactUs_Email_Network_User"]) ? new NetworkCredential() : new NetworkCredential(ConfigurationManager.AppSettings["ContactUs_Email_Network_User"].ToString(), ConfigurationManager.AppSettings["ContactUs_Email_Network_Password"].ToString());
                smtp.Timeout = 20*1000;

                smtp.Send(msg);
            }

            catch(SmtpFailedRecipientException e)
            {
                // Failure when sending to one of the recipients
                throw e;
            }

            catch (SmtpException e) 
            {
                // Catch all - most likely connection error

                // As per http://msdn.microsoft.com/en-us/library/swas0fwc.aspx
                // If you receive an SmtpException exception, check the StatusCode property to find 
                // the reason the operation failed. The SmtpException can also contain an inner exception 
                // that indicates the reason the operation failed.

                throw e;
            }

            finally
            {
                msg.Dispose();
                msg = null;
                smtp = null;
            }
            
        }

        public static bool IsValidEmail(string emailAddress)
        {
            return Regex.IsMatch(emailAddress,
                    @"^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                    @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
        }

        public static bool IsValidPhoneNumber(string phoneNumber)
        {
            return Regex.IsMatch(phoneNumber,
                    @"^([0-9\(\)\/\+ \-]*)$");
        }

        public static bool IsEmptyOrNull(string s)
        {
            return (s == null || s == "");
        }

    }

}
